package dao

import (
	"fmt"
	"github.com/liangchao/blogo/src/common/global"
	"github.com/liangchao/blogo/src/common/models"
)

type UserDao struct {
}

//func FindUserInfo(username string, password string) (*models.User, bool){
//	rows := global.DB.Where(&models.User{LoginUserName: username, LoginPassword:password}).Find(&user)
//	fmt.Println(&user)
//	if rows.RowsAffected < 1 {
//		return &user, false
//	}
//
//	return &user, true
//}

// FindUserByBid 通过 bid 查查 user
func (d *UserDao) FindUserByBid(bid int) (user *models.User, err error) {
	uid, err := new(models.UserBlogRelation).FindUidByBid(bid)
	user = &models.User{AdminUserId: uid.AdminUserId}
	user, err = user.FindUserById()
	return user, err
}

var user models.User

// FindUserInfo 查询用户信息
func (d *UserDao) FindUserInfo(username string, password string) (*models.User, bool) {
	rows := global.DB.Where(&models.User{LoginUserName: username, LoginPassword: password}).Find(&user)
	fmt.Println(&user)
	if rows.RowsAffected < 1 {
		return &user, false
	}

	return &user, true
}

// FindUserById 通过id查询用户
func (d *UserDao) FindUserById(userId int) (*models.User, bool) {
	rows := global.DB.Where(&models.User{AdminUserId: userId}).Find(&user)
	if rows.RowsAffected < 1 {
		return &user, false
	}

	return &user, true
}
